

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/hh1247857703/BlogOfHH.git/img/favicon.png">
  <link rel="icon" href="/hh1247857703/BlogOfHH.git/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="HH">
  <meta name="keywords" content="">
  
    <meta name="description" content="转载文章 – EasyExcel与@Accessors(chain &#x3D; true)不兼容分析EasyExcelEasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。在尽可能节约内存的情况下支持读写百M的Excel. github地址: https:&#x2F;&#x2F;github.com&#x2F;alibaba&#x2F;easyexcel Accessors@Accessors 注解用来配置lo">
<meta property="og:type" content="article">
<meta property="og:title" content="转载文章 -- EasyExcel与@Accessors(chain &#x3D; true)不兼容分析.md">
<meta property="og:url" content="https://gitee.com/hh1247857703/BlogOfHH.git/2022/03/31/java/EasyExcel%E4%B8%8E@Accessors(chain%20=%20true)%E4%B8%8D%E5%85%BC%E5%AE%B9%E5%88%86%E6%9E%90/index.html">
<meta property="og:site_name" content="The Blog Of HH">
<meta property="og:description" content="转载文章 – EasyExcel与@Accessors(chain &#x3D; true)不兼容分析EasyExcelEasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。在尽可能节约内存的情况下支持读写百M的Excel. github地址: https:&#x2F;&#x2F;github.com&#x2F;alibaba&#x2F;easyexcel Accessors@Accessors 注解用来配置lo">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200816134215934.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200816134651450.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200816144747553.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI4MDM2MjQ5,size_16,color_FFFFFF,t_70#pic_center">
<meta property="article:published_time" content="2022-03-31T03:25:30.000Z">
<meta property="article:modified_time" content="2022-03-31T06:39:09.708Z">
<meta property="article:author" content="HH">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="Spring">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/20200816134215934.png#pic_center">
  
  
  <title>转载文章 -- EasyExcel与@Accessors(chain = true)不兼容分析.md - The Blog Of HH</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/arta.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/hh1247857703/BlogOfHH.git/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"gitee.com","root":"/hh1247857703/BlogOfHH.git/","version":"1.8.14","typing":{"enable":true,"typeSpeed":130,"cursorChar":"💨","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":"❡"},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"baidu":17795511,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/hh1247857703/BlogOfHH.git/local-search.xml"};
  </script>
  <script  src="/hh1247857703/BlogOfHH.git/js/utils.js" ></script>
  <script  src="/hh1247857703/BlogOfHH.git/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 6.1.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/hh1247857703/BlogOfHH.git/">
      <strong>Smile Everyday!</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hh1247857703/BlogOfHH.git/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hh1247857703/BlogOfHH.git/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hh1247857703/BlogOfHH.git/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hh1247857703/BlogOfHH.git/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hh1247857703/BlogOfHH.git/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/hh1247857703/BlogOfHH.git/img/allbg.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="转载文章 -- EasyExcel与@Accessors(chain = true)不兼容分析.md">
              
            </span>

            
              <div class="mt-3">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-author" aria-hidden="true"></i>
      HH
    </span>
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2022-03-31 11:25" pubdate>
        2022年3月31日 上午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      4.8k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      40 分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">转载文章 -- EasyExcel与@Accessors(chain = true)不兼容分析.md</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：2 天前
                
              </p>
            
            <div class="markdown-body">
              <h1 id="转载文章-–-EasyExcel与-Accessors-chain-x3D-true-不兼容分析"><a href="#转载文章-–-EasyExcel与-Accessors-chain-x3D-true-不兼容分析" class="headerlink" title="转载文章 – EasyExcel与@Accessors(chain &#x3D; true)不兼容分析"></a>转载文章 – EasyExcel与@Accessors(chain &#x3D; true)不兼容分析</h1><h2 id="EasyExcel"><a href="#EasyExcel" class="headerlink" title="EasyExcel"></a>EasyExcel</h2><p>EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。在尽可能节约内存的情况下支持读写百M的Excel. github地址: <a target="_blank" rel="noopener" href="https://github.com/alibaba/easyexcel">https://github.com/alibaba/easyexcel</a></p>
<h2 id="Accessors"><a href="#Accessors" class="headerlink" title="Accessors"></a>Accessors</h2><p>@Accessors 注解用来配置lombok如何产生和显示getters和setters的方法。</p>
<blockquote>
<p>public @interface Accessors {<br>    &#x2F;**<br>     * 如果为true，则访问器将以该字段命名，并且不包含 get 或 set *前缀,且省略了chain，则 chain 默认为true。<br>     * *默认值：false<br>     *&#x2F;<br>    boolean fluent() default false;</p>
</blockquote>
<h2 id="测试案例"><a href="#测试案例" class="headerlink" title="测试案例"></a>测试案例</h2><p>pom.xml</p>
<figure class="highlight xml"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></div></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.projectlombok<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>lombok<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">optional</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">optional</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.alibaba<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>easyexcel<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.2.6<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p>Student.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Data</span><br><span class="hljs-meta">@Accessors(chain = true)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Student</span> &#123;<br><br>    <span class="hljs-meta">@ExcelProperty(&quot;编码&quot;)</span><br>    <span class="hljs-keyword">private</span> Long id;<br><br>    <span class="hljs-meta">@ExcelProperty(&quot;姓名&quot;)</span><br>    <span class="hljs-keyword">private</span> String name;<br><br>    <span class="hljs-meta">@ExcelProperty(&quot;性别&quot;)</span><br>    <span class="hljs-keyword">private</span> String sex;<br><br>    <span class="hljs-meta">@ExcelProperty(&quot;年龄&quot;)</span><br>    <span class="hljs-keyword">private</span> Integer age;<br><br>&#125;<br></code></pre></td></tr></table></figure>
<p>StudentControllerTest.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">StudentControllerTest</span> &#123;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 导出Excel上传模板</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">importTemplate</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">filePath</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;D:/student.xlsx&quot;</span>;<br>        EasyExcel.write(filePath, Student.class).sheet(<span class="hljs-string">&quot;student&quot;</span>).doWrite(<span class="hljs-literal">null</span>);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 导入Excel数据</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">importExcel</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">filePath</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;D:/student.xlsx&quot;</span>;<br>        List&lt;Object&gt; list = EasyExcel.read(filePath, Student.class, <span class="hljs-literal">null</span>).sheet(<span class="hljs-string">&quot;student&quot;</span>).doReadSync();<br>        <span class="hljs-keyword">for</span> (Object obj : list) &#123;<br>            System.out.println(obj.toString());<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>student.xlsx 数据</p>
<p><img src="https://img-blog.csdnimg.cn/20200816134215934.png#pic_center" srcset="/hh1247857703/BlogOfHH.git/img/loading/loading.gif" lazyload></p>
<p>执行 importExcel 导入Excel 结果: 可以看到, 导入数据条数能解析, 但数据没有读取成功</p>
<p><img src="https://img-blog.csdnimg.cn/20200816134651450.png#pic_center" srcset="/hh1247857703/BlogOfHH.git/img/loading/loading.gif" lazyload></p>
<h2 id="原因详解"><a href="#原因详解" class="headerlink" title="原因详解"></a>原因详解</h2><p>通过删减无关代码的方法, 发现当删减 @Accessors(chain &#x3D; true)或修改为 @Accessors(chain &#x3D; false)后, 能够正常读取到了, 定位到是该注解的影响, 为什么会影响呢? 带着这个疑问我们继续往下看</p>
<h3 id="EasyExcel-解析Excel-过程"><a href="#EasyExcel-解析Excel-过程" class="headerlink" title="EasyExcel 解析Excel 过程"></a>EasyExcel 解析Excel 过程</h3><p>这里不就过多赘述了, 大家可以查看如下两个文章进行了解</p>
<p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/gwtjava/p/11937777.html">EasyExcel对Excel文件的解析过程</a><br><a target="_blank" rel="noopener" href="https://blog.csdn.net/baidu_21349635/article/details/106158100">easyexcel的源码简单分析</a></p>
<h3 id="ModelBuildEventListener"><a href="#ModelBuildEventListener" class="headerlink" title="ModelBuildEventListener"></a>ModelBuildEventListener</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">private</span> Object <span class="hljs-title function_">buildUserModel</span><span class="hljs-params">(Map&lt;Integer, CellData&gt; cellDataMap, ReadHolder currentReadHolder,</span><br><span class="hljs-params">        AnalysisContext context)</span> &#123;<br>        <span class="hljs-type">ExcelReadHeadProperty</span> <span class="hljs-variable">excelReadHeadProperty</span> <span class="hljs-operator">=</span> currentReadHolder.excelReadHeadProperty();<br>        Object resultModel;<br>        <span class="hljs-keyword">try</span> &#123;<br>            resultModel = excelReadHeadProperty.getHeadClazz().newInstance();<br>        &#125; <span class="hljs-keyword">catch</span> (Exception e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ExcelDataConvertException</span>(context.readRowHolder().getRowIndex(), <span class="hljs-number">0</span>,<br>                <span class="hljs-keyword">new</span> <span class="hljs-title class_">CellData</span>(CellDataTypeEnum.EMPTY), <span class="hljs-literal">null</span>,<br>                <span class="hljs-string">&quot;Can not instance class: &quot;</span> + excelReadHeadProperty.getHeadClazz().getName(), e);<br>        &#125;<br>        Map&lt;Integer, Head&gt; headMap = excelReadHeadProperty.getHeadMap();<br>        Map&lt;String, Object&gt; map = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;String, Object&gt;(headMap.size() * <span class="hljs-number">4</span> / <span class="hljs-number">3</span> + <span class="hljs-number">1</span>);<br>        Map&lt;Integer, ExcelContentProperty&gt; contentPropertyMap = excelReadHeadProperty.getContentPropertyMap();<br>        <span class="hljs-keyword">for</span> (Map.Entry&lt;Integer, Head&gt; entry : headMap.entrySet()) &#123;<br>            <span class="hljs-type">Integer</span> <span class="hljs-variable">index</span> <span class="hljs-operator">=</span> entry.getKey();<br>            <span class="hljs-keyword">if</span> (!cellDataMap.containsKey(index)) &#123;<br>                <span class="hljs-keyword">continue</span>;<br>            &#125;<br>            <span class="hljs-type">CellData</span> <span class="hljs-variable">cellData</span> <span class="hljs-operator">=</span> cellDataMap.get(index);<br>            <span class="hljs-keyword">if</span> (cellData.getType() == CellDataTypeEnum.EMPTY) &#123;<br>                <span class="hljs-keyword">continue</span>;<br>            &#125;<br>            <span class="hljs-type">ExcelContentProperty</span> <span class="hljs-variable">excelContentProperty</span> <span class="hljs-operator">=</span> contentPropertyMap.get(index);<br>            <span class="hljs-type">Object</span> <span class="hljs-variable">value</span> <span class="hljs-operator">=</span> ConverterUtils.convertToJavaObject(cellData, excelContentProperty.getField(),<br>                excelContentProperty, currentReadHolder.converterMap(), currentReadHolder.globalConfiguration(),<br>                context.readRowHolder().getRowIndex(), index);<br>            <span class="hljs-keyword">if</span> (value != <span class="hljs-literal">null</span>) &#123;<br>                map.put(excelContentProperty.getField().getName(), value);<br>            &#125;<br>        &#125;<br>        BeanMap.create(resultModel).putAll(map);<br>        <span class="hljs-keyword">return</span> resultModel;<br>    &#125;<br></code></pre></td></tr></table></figure>
<p>当Excel每行数据解析后, 会调用 buildStringList 将解析到的数据由Map转存到Bean中, 关键代码如下:</p>
<blockquote>
<p>BeanMap.create(resultModel).putAll(map);</p>
</blockquote>
<p>可见, EasyExcel是使用 <em>net.sf.cglib.beans.BeanMap</em> 工具类拷贝的, 这正是造成 使用 <em>@Accessors(chain &#x3D; true)</em> 后, <strong>EasyExcel解析不到数据的原因所在.</strong></p>
<h3 id="为什么使用了-Accessors-chain-x3D-true-后-BeanMap会拷贝不成功呢"><a href="#为什么使用了-Accessors-chain-x3D-true-后-BeanMap会拷贝不成功呢" class="headerlink" title="为什么使用了@Accessors(chain &#x3D; true)后, BeanMap会拷贝不成功呢?"></a>为什么使用了@Accessors(chain &#x3D; true)后, BeanMap会拷贝不成功呢?</h3><h4 id="Accessors-chain-x3D-true-的作用"><a href="#Accessors-chain-x3D-true-的作用" class="headerlink" title="@Accessors(chain &#x3D; true) 的作用"></a>@Accessors(chain &#x3D; true) 的作用</h4><p>使set方法返回的不是void, 而是当前对象, 例如:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 不加 @Accessors(chain = true) 时</span><br><span class="hljs-meta">@Data</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Student</span> &#123;<br>	<span class="hljs-keyword">private</span> String name;<br>&#125;<br><span class="hljs-comment">// 相当于</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Student</span> &#123;<br>	<span class="hljs-keyword">private</span> String name;<br>	<br>	<span class="hljs-keyword">public</span> String <span class="hljs-title function_">getName</span><span class="hljs-params">()</span> &#123;<br>		<span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.name;<br>	&#125;<br>	<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setName</span><span class="hljs-params">(String name)</span> &#123;<br>		<span class="hljs-built_in">this</span>.name = name;<br>	&#125;<br>&#125;<br><br><span class="hljs-comment">// 加了 @Accessors(chain = true) 时</span><br><span class="hljs-meta">@Data</span><br><span class="hljs-meta">@Accessors(chain = true)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Student</span> &#123;<br>	<span class="hljs-keyword">private</span> String name;<br>&#125;<br><span class="hljs-comment">// 相当于, 添加的好处: 可以支持 new Student().setName(&quot;zhang&quot;).setXXX(&quot;xxx&quot;).setXXXX(&quot;XXX&quot;)这样的操作</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Student</span> &#123;<br>	<span class="hljs-keyword">private</span> String name;<br>	<br>	<span class="hljs-keyword">public</span> String <span class="hljs-title function_">getName</span><span class="hljs-params">()</span> &#123;<br>		<span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.name;<br>	&#125;<br>	<span class="hljs-keyword">public</span> Student <span class="hljs-title function_">setName</span><span class="hljs-params">(String name)</span> &#123;<br>		<span class="hljs-built_in">this</span>.name = name;<br>		<span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>;<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="BeanMap-从Map拷贝到Bean的测试"><a href="#BeanMap-从Map拷贝到Bean的测试" class="headerlink" title="BeanMap 从Map拷贝到Bean的测试"></a>BeanMap 从Map拷贝到Bean的测试</h3><p>参考: 使<a target="_blank" rel="noopener" href="https://blog.csdn.net/zmx729618/article/details/78363191">用CGlib实现Bean拷贝（BeanCopier</a><br>可见, 需要使用BeanMap从Map拷贝到Bean, 需要Map 的Key与Bean的变量名一致, 并有对应的 set方法, 且set方法为 void, 才能拷贝成功</p>
<p>已经找到了原因, 如何解决呢? 到GitHub里看了看, 作者表示现在还没法解决, 小编只能暂时不用 @Accessors(chain &#x3D; true), 还是提醒广大网友, 在使用MybatisPlus生成代码时,一定要注意是否带有@Accessors(chain &#x3D; true)注解,如果带有,会影响EasyExcel读取数据哦</p>
<p><img src="https://img-blog.csdnimg.cn/20200816144747553.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI4MDM2MjQ5,size_16,color_FFFFFF,t_70#pic_center" srcset="/hh1247857703/BlogOfHH.git/img/loading/loading.gif" lazyload></p>
<blockquote>
<p>以上内容转载自:<a target="_blank" rel="noopener" href="https://www.cnblogs.com/yangxiongbin/articles/14644476.html">https://www.cnblogs.com/yangxiongbin/articles/14644476.html</a>, 如果冒犯，请联系我删除!</p>
</blockquote>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/hh1247857703/BlogOfHH.git/categories/%E8%BD%AC%E8%BD%BD/">转载</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/hh1247857703/BlogOfHH.git/tags/Java/">Java</a>
                    
                      <a class="hover-with-bg" href="/hh1247857703/BlogOfHH.git/tags/Spring/">Spring</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/hh1247857703/BlogOfHH.git/2022/03/31/java/%E4%BD%BF%E7%94%A8SpringBoot%20AOP%20%E8%AE%B0%E5%BD%95%E6%93%8D%E4%BD%9C%E6%97%A5%E5%BF%97%E3%80%81%E5%BC%82%E5%B8%B8%E6%97%A5%E5%BF%97/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">使用SpringBoot AOP 记录操作日志、异常日志.md</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/hh1247857703/BlogOfHH.git/2022/03/30/software/%E8%A3%85%E6%9C%BA%E8%BD%AF%E4%BB%B6/">
                        <span class="hidden-mobile">装机软件.md</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments" lazyload>
                
                  
                
                
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://cdn.jsdelivr.net/npm/valine@1/dist/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"YNaQpUHmK70UWg4eAuxASf1T-gzGzoHsz","appKey":"e0uHoSbv8bBD5Jruur36RDvW","path":"window.location.pathname","placeholder":null,"avatar":"robohash","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":false,"recordIP":false,"serverURLs":"","emojiCDN":null,"emojiMaps":null,"enableQQ":false},
          {
            el: "#valine",
            path: window.location.pathname
          }
        )
        new Valine(options);
        Fluid.utils.waitElementVisible('#valine .vcontent', () => {
          Fluid.plugins.initFancyBox('#valine .vcontent img:not(.vemoji)');
        })
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>本网站使用Fluid主题</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/hh1247857703/BlogOfHH.git/js/events.js" ></script>
<script  src="/hh1247857703/BlogOfHH.git/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/hh1247857703/BlogOfHH.git/js/local-search.js" ></script>



  
    <script  src="/hh1247857703/BlogOfHH.git/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>












  
    <!-- Baidu Analytics -->
    <script defer>
      var _hmt = _hmt || [];
      (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?17795511";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
      })();
    </script>
  

  

  

  

  

  





<!-- 主题的启动项 保持在最底部 -->
<script  src="/hh1247857703/BlogOfHH.git/js/boot.js" ></script>


</body>
</html>
